#include<iostream>
#include<cstring>
#include<vector>
#include<cstdio>
using namespace std;
const int maxn = 200005;

int n,ans=0x7ffffff,numlist[maxn],tot;

int main(){

    cin>>n;
    int tmp;
    for(int i=1;i<=n;i++){
        cin>>tmp;
        numlist[i]=tmp;
    }
    
    
    
    for(int i=1;i<=n;i++){
        int now=i;
        int num=0;
        while(i!=numlist[now]){
            tot++;
            now=numlist[now];
            num++;
            if(num>n+1 || num>ans){
                num=0x7ffffff;
                break;
            }
        }
        if(tot>5000000)break;
        num++;
        ans=min(ans,num);
    }
    cout<<ans<<endl;
}